import logger from 'loglevel';
import { IconLayer } from '../core/map/layers/IconLayer';
import { IBizUnitConfig } from '../core';
import { BizUnit } from '../core';
/**
 * 自定义业务单元类，继承自 BizUnit
 * 该类用于处理告警逻辑
 */
export class WarningBizUnit extends BizUnit {
  override sizeScale = 0.5;
  unitType = 'warning'; // 业务单元类型

  override async updateGeoData(
    cfg: IBizUnitConfig | null,
    data?: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[],
  ): Promise<void> {
    console.log('无用的配置数据', cfg);
    const features =
      data ||
      ([
        {
          type: 'Feature',
          geometry: {
            type: 'Point',
            coordinates: [116.98392791512401, 36.66390112537448],
          },
          id: 'warning.1',
          properties: {
            name: '告警点1',
            type: 'warning',
            rt_state: 'normal',
          },
        },
        {
          type: 'Feature',
          id: 'warning.2',
          geometry: {
            type: 'Point',
            coordinates: [116.99448508980669, 36.647100072818034],
          },
          properties: {
            name: '告警点2',
            type: 'warning',
            rt_state: 'error',
          },
        },
      ] as GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[]);
    this.engiGis.addOrUpdateSource(this.sourceId, {
      type: 'geojson',
      data: {
        type: 'FeatureCollection',
        features,
      },
    });
  }

  async setRenderer(config: IBizUnitConfig): Promise<IconLayer[]> {
    logger.info('开始绘制告警图层');
    const engiGis = this.engiGis;
    const icons = {
      'warning-normal': 'public/icons/warning.png',
      'warning-error': 'public/icons/warning-error.png',
    };
    await engiGis.mapCore.loadImages(icons);
    logger.info('告警图标加载完成', icons);
    const iconLayer = new IconLayer(
      {
        layerId: this.layerId,
        sourceId: this.sourceId,
        map: this.map,
        sort: config.sort || 0,
      },
      {
        layout: {
          ...this.getLayoutBase(),
          'icon-image': [
            'coalesce',
            ['image', ['concat', ['get', 'type'], '-', ['get', 'rt_state']]], // 根据状态显示图标
            // ['image', 'heatplant-ready'] // 默认图标
          ],
          'symbol-sort-key': config.sort || 0,
          'icon-size': this.sizeScale,
          'icon-allow-overlap': true,
          'icon-ignore-placement': true,
          visibility: 'visible', // 确保图层可见
        },
      },
    );
    this.layers.push(iconLayer);
    return [iconLayer];
  }
}
